package com.foreknow.service.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.model.Admin;
import com.foreknow.model.Guestbook;
import com.foreknow.service.AdminService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-05 14:13
 */
public class AdminServiceImpl implements AdminService {
    private AdminDaoImpl dao1 = new AdminDaoImpl();

    //调用的是BaseDAO中的setConnection(conn)方法

    @Override
    public Admin login(String username, String password) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao1.setConnection(conn);
        Admin admin=null;
        try {
            admin= dao1.isLogin(username, password);
            if(admin!=null){
                return admin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean register(Admin admin) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao1.setConnection(conn);
        try {
            boolean register=dao1.register(admin);
            return register;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Object> showAll() {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao1.setConnection(connection);
        try {
            List<Object> all = dao1.getAll();
            return all;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }


}
